package com.wyh.service.serviceImpl.groupOne.customer;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.wyh.common.enums.ResultCodeEnum;
import com.wyh.common.exception.CustomException;
import com.wyh.domain.vo.groupOne.customer.MasterDataCustomerVO;
import com.wyh.domain.vo.groupOne.customer.Feenotificationconfiguration;
import com.wyh.mapper.groupOne.customer.MasterDataCCustomerMapper;
import com.wyh.mapper.groupOne.customer.MasterDataFeenotificationconfigurationMapper;
import com.wyh.service.groupOne.customer.MasterDataFeenotificationconfigurationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MasterDataFeenotificationconfigurationServiceImpl implements MasterDataFeenotificationconfigurationService {

    @Autowired
    private MasterDataFeenotificationconfigurationMapper masterDataFeenotificationconfigurationMapper;

    @Autowired
    private MasterDataCCustomerMapper MasterDataCCustomerMapper;

    @Override
    public int update(Feenotificationconfiguration feenotificationconfiguration) {

        MasterDataCustomerVO masterDataCustomerVO = MasterDataCCustomerMapper.selectDetailBasic(feenotificationconfiguration.getId());
        if (!ObjectUtil.isEmpty(masterDataCustomerVO) && ObjectUtil.isNotEmpty(feenotificationconfiguration.getUserid())) {
            DateTime date = DateUtil.date();
            masterDataCustomerVO.setLastupdatetime(date.toString());
            masterDataCustomerVO.setUserid(feenotificationconfiguration.getUserid());
            MasterDataCCustomerMapper.update(masterDataCustomerVO);
            return masterDataFeenotificationconfigurationMapper.update(feenotificationconfiguration);
        }
        throw new CustomException(ResultCodeEnum.OPERATION_AUTHORITY_ERROR);
    }

    @Override
    public int insert(Feenotificationconfiguration feenotificationconfiguration) {
        MasterDataCustomerVO dbcustomer = MasterDataCCustomerMapper.selectDetailBasic(feenotificationconfiguration.getId());
        Feenotificationconfiguration dbfee = masterDataFeenotificationconfigurationMapper.selectById(feenotificationconfiguration.getId());
        if (!ObjectUtil.isEmpty(dbfee)){
            throw new CustomException(ResultCodeEnum.PROJECT_NAME_EXIST);
        }
        if (!ObjectUtil.isEmpty(dbcustomer) && ObjectUtil.isNotEmpty(feenotificationconfiguration.getUserid())) {
            DateTime date = DateUtil.date();
            dbcustomer.setLastupdatetime(date.toString());
            dbcustomer.setUserid(feenotificationconfiguration.getUserid());
            MasterDataCCustomerMapper.update(dbcustomer);
            return masterDataFeenotificationconfigurationMapper.insert(feenotificationconfiguration);
        }
        throw new CustomException(ResultCodeEnum.OPERATION_AUTHORITY_ERROR);
    }

    @Override
    public Feenotificationconfiguration selectById(Integer id) {
        if(id != null){
            return masterDataFeenotificationconfigurationMapper.selectById(id);
        }
        throw new CustomException(ResultCodeEnum.PARAM_LOST_ERROR);
    }
}
